package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    @Autowired
    WmSensitiveMapper wmSensitiveMapper;

    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        //入参判断,如果入参是空值
        if (wmSensitive == null || StringUtils.isEmpty(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //如果关键词在数据库中已存在
        WmSensitive sensitive = wmSensitiveMapper.selectOne(Wrappers.<WmSensitive>lambdaQuery().
                eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (sensitive != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //
        wmSensitive.setCreatedTime(new Date());
        //存入关键词
        boolean save = save(wmSensitive);
        if (!save) {
            throw new RuntimeException("WmSensitiveServiceImpl----->关键词存入失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult deleteSensitive(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmSensitiveMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        //入参判断
        if (wmSensitive == null
                || StringUtils.isEmpty(wmSensitive.getSensitives())
                || wmSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmSensitiveMapper.updateById(wmSensitive);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }


    final static int MAX_SIZE = 50;
    final static int DEFAULT_SIZE = 10;
    final static int DEFAULT_PAGE = 1;

    @Override
    public ResponseResult selectSensitive(SensitiveDto sensitiveDto) {
        //入参判断
        if (sensitiveDto == null) {
            return PageResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //如果入参的size和page是空或者是其他离谱的数据,需要约束一下
        Integer page = sensitiveDto.getPage();
        Integer size = sensitiveDto.getSize();
        if (page == 0) {
            page = DEFAULT_PAGE;
        }
        if (size == 0 || size < 10) {
            size = DEFAULT_SIZE;
        }
        Math.min(page, MAX_SIZE);
        //如果有关键词名称,则查询单个关键词
        LambdaQueryWrapper<WmSensitive> lqw = new LambdaQueryWrapper<>();
        lqw.like(!StringUtils.isEmpty(sensitiveDto.getName()), WmSensitive::getSensitives, sensitiveDto.getName());
        //初始化分页对象
        Page<WmSensitive> pageInfo = new Page<>(page, size);
        page(pageInfo, lqw);
        //返回
        ResponseResult responseResult = new PageResponseResult(page, size, (int) pageInfo.getTotal());
        responseResult.setData(pageInfo.getRecords());
        return responseResult;
    }

}
